package codeRandomThoughts.Test503下一个最大元素II;

import java.util.Arrays;
import java.util.Stack;

public class Solution2 {
    public int[] nextGreaterElements(int[] nums) {
        //单调栈
        Stack<Integer> s = new Stack<>();
        int len = nums.length;
        int[] res = new int[len];
        Arrays.fill(res, -1);

        //循环数组 遍历的时候要注意
        for (int i = 0; i < len * 2 - 1; i++) {
            while (!s.isEmpty() && (nums[s.peek()] < nums[i % len])) {
                //弹出的是下标
                Integer pop = s.pop();
                res[pop] = nums[i % len];
            }

            s.push(i % len);
        }

        return res;
    }
}
